import xorEncryptionData from "./Tool/xor";
import httpUtils from "./XmlHttpTool";
import allVariableJs from "./allVariable";
import PlatformUtil from "./PlatformUtil";
import NumberView from "./NumberView";
cc.Class({
    extends: cc.Component,

    properties: {
       
        endAgainGameBtn:cc.Button,
        backGameMapBtn:cc.Button,
        backMarkView:cc.Node,
        closeBtn:cc.Button,
        endSound:{
            default: null,
            type: cc.AudioClip
          },
        endallview:cc.Node,
        bglightcoin:cc.Node,
        bglightNocoin:cc.Node,
        againGame:cc.Button,
        energyCompontent:NumberView,
        restBtn:cc.Button
   
    },


    againOnceGameClick:function(){

        sdkbox.PluginAdMob.hide("home")
        var changebig = cc.scaleTo(0.2,1.1);

        var finish = cc.callFunc(function(){
    
            var that=this
          var changesmail = cc.scaleTo(0.1,1.0);

          var smaillfinish=cc.callFunc(function(){

            
            that.endallview.active=false

            console.log("重新开始游戏")
    
            cc.director.loadScene("GameSence",function(){
    
                 var mainNode= cc.find("Canvas/backgroundtop")
    
                 mainNode.getComponent("backgrould").onAgainSence();
            
            });

          })

          var seq2 = cc.sequence(changesmail,smaillfinish)
          this.againGame.node.runAction(seq2)
       

       }, this);
    
       var seq = cc.sequence(changebig,finish)
        
       this.againGame.node.runAction(seq)

     

    },

    closeGameClick:function(){

        var changebig = cc.scaleTo(0.02,1.2);

        var finish = cc.callFunc(function(){
    
          var changesmail = cc.scaleTo(0.08,1.0);

          var smaillfinish=cc.callFunc(function(){

            cc.director.loadScene("StartGame");

          })

          var seq2 = cc.sequence(changesmail,smaillfinish)
          this.closeBtn.node.runAction(seq2)
       

       }, this);
    
       var seq = cc.sequence(changebig,finish)

       this.closeBtn.node.runAction(seq)

    },

    backGameMapClick:function(){

        var changebig = cc.scaleTo(0.2,1.1);

        var finish = cc.callFunc(function(){
    
          var changesmail = cc.scaleTo(0.1,1.0);

          var smaillfinish=cc.callFunc(function(){


          })

          var seq2 = cc.sequence(changesmail,smaillfinish)
          this.restBtn.node.runAction(seq2)
       

       }, this);
    
       var seq = cc.sequence(changebig,finish)

       this.restBtn.node.runAction(seq)

       sdkbox.PluginAdColony.show("v4vc");

    },


    onLoad () {

        sdkbox.PluginAdMob.show("home")
        this.sendStarHttp();
    
        this.backMarkView.on('touchstart', function (event) {
            console.log("点击结算背景层")
          }, this);

      var that =this
      
      cc.loader.load({url: "https://h5.taoqi001.com/upload/201812/9834e678f3e8c6636778849dd055579c320f0900.png", type: 'png'}, function (err, img) {
        var mylogo  = new cc.SpriteFrame(img); 
        that.bglightcoin.getComponent(cc.Sprite).spriteFrame = mylogo;
        that.bglightcoin.height=432
        that.bglightcoin.width=712

        that.bglightNocoin.getComponent(cc.Sprite).spriteFrame = mylogo;
        that.bglightNocoin.height=432
        that.bglightNocoin.width=712

      });

      var that=this
      sdkbox.PluginAdColony.setListener({
          onAdColonyChange : function (data, available) {
          
              console.log("启动onAdColonyChange",data,available)
          
           
          },
          onAdColonyReward : function (data, currencyName, amount, success) {
              // Called when AdColony v4vc ad finish playing
           
              console.log("onAdColonyReward",data,available)
           

          },
          onAdColonyStarted : function (data) {
              // Called when ad starts playing
   
              console.log("onAdColonyStarted",data,available)
            
          },
          onAdColonyFinished : function (data) {
              // Called when an ad finish displaying
  
              console.log("广告结束")

              var _this=that
              httpUtils.httpPost("/marmot/ads_to_energy",{},function(res){
     
                
                  var flagstr=res.flag_key+PlatformUtil.platform.params["appver"]
                  var resultstr =xorEncryptionData.encryptionData(flagstr,PlatformUtil.platform.params)
                //   console.log("正常播放,可以发游戏奖励",JSON.stringify(res))
         
                
                console.log("正常播放,可以发游戏奖励",flagstr)
                  httpUtils.NhttpPost("/marmot/ads_to_energy",{"flag_key":resultstr,"keysid":res.keysid},function(res){
                    console.log("广告体力请求接口",JSON.stringify(res))
                 _this.energyCompontent.setText(parseInt(res["energy"]))
                
                    
                },function(failres){
                    console.log("广告体力请求接口失败",JSON.stringify(failres))  
                })         
              }) 
          }
      });


    },


    sendStarHttp:function(){

        allVariableJs.isPlaying=false
      
        var resultDic={"coins":allVariableJs.wawaCoinAddNumber, "game_id":allVariableJs.gameId,"stars":allVariableJs.starNumber,"hammers":allVariableJs.chuiziCount,"add_time":allVariableJs.timeCount,"game_time":allVariableJs.myPlayTimes}
        console.log("结束游戏",JSON.stringify(resultDic))

        this.current = cc.audioEngine.play(this.endSound, false, 1);
        
        var resultstr =xorEncryptionData.encryptionData(resultDic,PlatformUtil.platform.params)

          var that=this
          
           httpUtils.ThttpPost("/marmot/catch_callback",{"result":resultstr},function(res){

            console.log("上传分数",JSON.stringify(res))

            let score = allVariableJs.starNumber;
            if (CC_WECHATGAME) {
                console.log("上传排行榜")
                window.wx.postMessage({
                    messageType: 3,
                    MAIN_MENU_NUM: "x1",
                    score: score,
                });
            } else {
                cc.log("提交得分: x1 : " + score)
            }

            if(allVariableJs.wawaCoinAddNumber==0){
               
                if(parseInt(res["data"]["user_stars"])<0){

                    var starabs=Math.abs(res["data"]["user_stars"])
                    that.node.getChildByName("gameStarLabel").getChildByName("miusstarNumber").getComponent("NumberView").setLeftText(parseInt(starabs),true)
                    console.log("星币为负数",starabs)

                }else{
                    that.node.getChildByName("gameStarLabel").getChildByName("starNumberCreator").getComponent("NumberView").setLeftText(parseInt(res["data"]["user_stars"]),false)
                }
             
            }else{
                if(parseInt(res["data"]["user_stars"])<0){
                    
                    var starabs=Math.abs(res["data"]["user_stars"])
                    console.log("星币为负数",starabs)
                    that.node.getChildByName("gameStarLabel").getChildByName("miusstarNumber").getComponent("NumberView").setLeftText(parseInt(starabs),true)
        
                }else{
                    that.node.getChildByName("gameStarLabel").getChildByName("starNumberCreator").getComponent("NumberView").setLeftText(parseInt(res["data"]["user_stars"]),false)
                }     
                that.node.getChildByName("gameCoinLabel").getChildByName("coinNumberCreator").getComponent("NumberView").setLeftText(parseInt(res["data"]["coins"]),false)
            }

           },function(failres){

            console.log("上传分数错误",JSON.stringify(failres))
           })

    },

    start () {

    },

    // update (dt) {},
});
